package cn.gz.demo.ui;


import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;

import java.util.ArrayList;

import cn.gz.demo.R;
import cn.gz.demo.databinding.ViewSwitcherBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Lyz on 2017/3/8.
 */
public class ViewSwitcherActivity extends BaseActivity {
    private ViewSwitcherBinding binding;
    final private static String TAG = "Toby_Test";
    final private static int NUMBER_PRE_SCREEN = 12;

    private static class DataItem {
        String dataName;
        Drawable drawable;
    }

    private ArrayList<DataItem> items = new ArrayList<>();

    private int screenNo = -1;
    private int screenCount;

    ViewSwitcher viewSwitcher;

    LayoutInflater inflater;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_view_switcher);

        initView();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initView() {
        inflater = LayoutInflater.from(this);

        for (int i = 0; i < 40; ++i) {
            String label = String.valueOf(i);
            Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher, null);
            DataItem item = new DataItem();
            item.dataName = label;
            item.drawable = drawable;
            items.add(item);
        }

        screenCount = items.size() % NUMBER_PRE_SCREEN == 0 ?
                items.size() / NUMBER_PRE_SCREEN : items.size() / NUMBER_PRE_SCREEN + 1;

        viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
        viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return inflater.inflate(R.layout.slide_list_view, null);
            }
        });
        next(null);
    }

    public void prev(View view) {
        if (screenNo > 0) {
            --screenNo;
            viewSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
            viewSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);
            ((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
            viewSwitcher.showPrevious();
        }
    }

    public void next(View view) {
        if (screenNo < screenCount - 1) {
            ++screenNo;
            viewSwitcher.setInAnimation(this, R.anim.slide_in_right);
            viewSwitcher.setOutAnimation(this, R.anim.slide_out_left);
            ((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
            viewSwitcher.showNext();
        }
    }

    private BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            if (screenNo == screenCount - 1 && items.size() % NUMBER_PRE_SCREEN != 0) {
                return items.size() % NUMBER_PRE_SCREEN;
            }
            return NUMBER_PRE_SCREEN;
        }

        @Override
        public DataItem getItem(int i) {
            return items.get(screenNo * NUMBER_PRE_SCREEN + i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (convertView == null) {
                view = inflater.inflate(R.layout.label_icon, null);
            }

            final DataItem dataItem = getItem(position);

            if (dataItem != null) {
                ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
                if (imageView != null) {
                    imageView.setImageDrawable(dataItem.drawable);
                }

                TextView textView = (TextView) view.findViewById(R.id.textView);
                if (textView != null) {
                    textView.setText(dataItem.dataName);
                }
            }

            return view;
        }
    };
}
